//
//  RHMePlantInfoCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/6.
//

#import "RHMePlantInfoCell.h"
#import "RHTools.h"

@interface RHMePlantInfoCell()

@property (nonatomic, strong) SDCycleScrollView *cycleView;

//今日充电
@property (nonatomic ,strong) QMUILabel *todayChgLab;
//累计充电
@property (nonatomic ,strong) QMUILabel *totalChgLab;
//今日放电
@property (nonatomic ,strong) QMUILabel *todayDchgLab;
//累计放电
@property (nonatomic ,strong) QMUILabel *totalDchgLab;

//日收益
@property (nonatomic ,strong) QMUILabel *dayIncomeLab;
//月收益
@property (nonatomic ,strong) QMUILabel *monthIncomeLab;
//年收益
@property (nonatomic ,strong) QMUILabel *yearIncomeLab;
//累计收益
@property (nonatomic ,strong) QMUILabel *totalIncomeLab;


@end
@implementation RHMePlantInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,0, 0, 0));
    }];
    
    UIImage *plantImgage = [UIImage imageNamed:@"me_plant_picture_default"];
    _cycleView = [[SDCycleScrollView alloc]init];
    _cycleView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    _cycleView.clipsToBounds = YES;
    _cycleView.placeholderImage = plantImgage;
    _cycleView.backgroundColor = kColorRGBA(240, 240, 240, 0.8);
    _cycleView.layer.cornerRadius = 8;
    [bgView addSubview:_cycleView];
    
    [_cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.equalTo(weakSelf.cycleView.mas_width).multipliedBy(plantImgage.size.height/plantImgage.size.width);
    }];
   
    
    //收益数据

    NSArray *earnTitles = @[@"昨日收益".rh_localized,@"本月收益".rh_localized,@"本年收益".rh_localized,@"累计收益".rh_localized];
    UIView *earnView = [self itemWithTypeTitle:@"收益数据".rh_localized icon:@"plant_info_earn" subTitles:earnTitles];
    [bgView addSubview:earnView];
    [earnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.cycleView.mas_bottom).offset(15);
    }];
    self.dayIncomeLab   = [earnView viewWithTag:100];
    self.monthIncomeLab = [earnView viewWithTag:101];
    self.yearIncomeLab  = [earnView viewWithTag:102];
    self.totalIncomeLab = [earnView viewWithTag:103];
    
    //充放统计
    NSArray *chgTitles = @[@"今日充电".rh_localized,@"累计充电".rh_localized,@"今日放电".rh_localized,@"累计放电".rh_localized];
    UIView *totalView = [self itemWithTypeTitle:@"充放统计".rh_localized icon:@"plant_info_chg"  subTitles:chgTitles];
    [bgView addSubview:totalView];
    [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(earnView);
        make.right.equalTo(earnView);
        make.top.equalTo(earnView.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
    self.todayChgLab  = [totalView viewWithTag:100];
    self.totalChgLab  = [totalView viewWithTag:101];
    self.todayDchgLab = [totalView viewWithTag:102];
    self.totalDchgLab = [totalView viewWithTag:103];
}



- (void)setModel:(RHMePlantDetailModel *)model
{
    _model = model;
    self.cycleView.imageURLStringsGroup = [model.imageUrl componentsSeparatedByString:@","];
    
   
    
    //货币种类 0:人民币(CNY)  1:美元(USD) 2:欧元(EUR) 3:日元(JPY) 4:英镑(GBP)  5:澳元(AUD)
    NSString *currencyType = [RHTools getCurrentCurrencyType];
    
    //今日收益
    NSString *todayIncomeValue = [NSDecimalNumber decimalStringWithDoubleString:model.yesterdayRevenue nullStr:@"--"];
    [self.dayIncomeLab setText:[NSString stringWithFormat:@"%@ %@",todayIncomeValue,currencyType]];
    self.dayIncomeLab.attributedText = [self attrStringWithContent:self.dayIncomeLab.text attrKey:currencyType];

   
   //月收益
    NSString *monthIncomeValue = [NSDecimalNumber decimalStringWithDoubleString:model.monthRevenue nullStr:@"--"];
    [self.monthIncomeLab setText:[NSString stringWithFormat:@"%@ %@",monthIncomeValue,currencyType]];
    self.monthIncomeLab.attributedText = [self attrStringWithContent:self.monthIncomeLab.text attrKey:currencyType];
    
    //年收益
     NSString *yearIncomeValue = [NSDecimalNumber decimalStringWithDoubleString:model.yearRevenue nullStr:@"--"];
     [self.yearIncomeLab setText:[NSString stringWithFormat:@"%@ %@",yearIncomeValue,currencyType]];
     self.yearIncomeLab.attributedText = [self attrStringWithContent:self.yearIncomeLab.text attrKey:currencyType];
    
    //累计收益
    NSString *totalIncomeValue = [NSDecimalNumber decimalStringWithDoubleString:model.accumulateRevenue nullStr:@"--"];
    [self.totalIncomeLab setText:[NSString stringWithFormat:@"%@ %@",totalIncomeValue,currencyType]];
    self.totalIncomeLab.attributedText = [self attrStringWithContent:self.totalIncomeLab.text attrKey:currencyType];
    
    
    
    //今日充电量
    NSString *todayChgValue = [NSDecimalNumber decimalStringWithDoubleString:model.todayCharge nullStr:@"--"];
    [self.todayChgLab setText:[NSString stringWithFormat:@"%@(kWh)",todayChgValue]];
    self.todayChgLab.attributedText = [self attrStringWithContent:self.todayChgLab.text attrKey:@"(kWh)"];
    
    //累计充电量
    NSString *totalChgValue = [NSDecimalNumber decimalStringWithDoubleString:model.accumulateCharge nullStr:@"--"];
    [self.totalChgLab setText:[NSString stringWithFormat:@"%@(kWh)",totalChgValue]];
    self.totalChgLab.attributedText = [self attrStringWithContent:self.totalChgLab.text attrKey:@"(kWh)"];
    
    //今日放电量
    NSString *todaylDchgValue = [NSDecimalNumber decimalStringWithDoubleString:model.todayDischarge nullStr:@"--"];
    [self.todayDchgLab setText:[NSString stringWithFormat:@"%@(kWh)",todaylDchgValue]];
    self.todayDchgLab.attributedText = [self attrStringWithContent:self.todayDchgLab.text attrKey:@"(kWh)"];
    
    //累计放电量
    NSString *totalDchgValue = [NSDecimalNumber decimalStringWithDoubleString:model.accumulateDischarge nullStr:@"--"];
    [self.totalDchgLab setText:[NSString stringWithFormat:@"%@(kWh)",totalDchgValue]];
    self.totalDchgLab.attributedText = [self attrStringWithContent:self.totalDchgLab.text attrKey:@"(kWh)"];
}



//收益数量 累计数据

-(UIView*)itemWithTypeTitle:(NSString*)title icon:(NSString*)icon  subTitles:(NSArray*)titles
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
    [self.contentView addSubview:bgView];
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
    titleLab.text = title;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    CGFloat itemWidth = bgWidth / 2;
    //左边第一个
    __block UIView *lastView;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = kView;
        [itemBgView addSubview:itemView];
        QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
        titleLab.text = title;
        titleLab.numberOfLines = 0;
        [itemView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.bottom.equalTo(itemView.mas_centerY).offset(-5);
        }];
        
        QMUILabel *contentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
        contentLab.text = @"--";
        contentLab.numberOfLines = 2;
        contentLab.tag = 100+idx;
        [itemView addSubview:contentLab];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.right.mas_offset(-5);
            make.top.equalTo(itemView.mas_centerY).offset(5);
        }];
        
        if (lastView) {
            if (idx%2==0) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(0);
                    make.size.equalTo(lastView);
                    make.top.equalTo(lastView.mas_bottom);
                }];
                //横线
                UIView *line_v = kView;
                line_v.backgroundColor = kCOLOR_HEX(@"#E3E6ED");
                [itemBgView addSubview:line_v];
                [line_v mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(20);
                    make.right.mas_offset(-20);
                    make.height.offset(1);
                    make.top.equalTo(lastView.mas_bottom).offset(-1);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right);
                    make.size.top.equalTo(lastView);
                }];
                //竖线
                UIView *line_h = kView;
                line_h.backgroundColor = kCOLOR_HEX(@"#E3E6ED");
                [itemBgView addSubview:line_h];
                [line_h mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lastView).offset(20);
                    make.bottom.equalTo(lastView).offset(-20);
                    make.width.offset(1);
                    make.centerX.mas_offset(0);
                }];
            }
            
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(0);
                make.width.offset(itemWidth);
                make.height.offset(76);
            }];
        }
        
        
        
        lastView = itemView;
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    return bgView;
}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}

@end
